package com.yimi.yinhepda.adapter.RecycleAdapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseViewHolder;
import com.pda.ym.groupedadapter.adapter.GroupedRecyclerViewAdapter;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.table.FuncBtnTable;
import com.yimi.yinhepda.entity.bean.FuncTeamEntity;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.views.main.function_module.AddFunctionActivity;

import java.util.List;


/**
 * 这是普通的分组Adapter 每一个组都有头部、尾部和子项。
 *
 * @author lj
 */
public class GroupedListAdapter extends GroupedRecyclerViewAdapter {

    private List<FuncTeamEntity> mGroups;
    private Context mContext;
    private boolean isDelete;

    public GroupedListAdapter(Context context, List<FuncTeamEntity> groups) {
        super(context);
        mGroups = groups;
        this.mContext = context;
    }

    @Override
    public int getGroupCount() {
        return mGroups == null ? 0 : mGroups.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<FuncBtnTable> children = mGroups.get(groupPosition).getTeamList();
        return children.size();
    }

    @Override
    public boolean hasHeader(int groupPosition) {
        return true;
    }

    @Override
    public boolean hasFooter(int groupPosition) {
        return true;
    }

    @Override
    public int getHeaderLayout(int viewType) {
        return R.layout.item_func_group;
    }

    @Override
    public int getFooterLayout(int viewType) {
        return R.layout.item_func_group_footer;
    }

    @Override
    public int getChildLayout(int viewType) {
        return R.layout.item_func_child;
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder holder, int groupPosition) {
        FuncTeamEntity entity = mGroups.get(groupPosition);
        holder.setText(R.id.tv_group_name, entity.getTeamName());
        //添加应用按钮
        View addView = holder.getView(R.id.ll_add);
        if (entity.getTeamName().equals(Common.FUNC_GROUP_OPTER)) {
            addView.setVisibility(View.VISIBLE);
        } else {
            addView.setVisibility(View.GONE);
        }

        addView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, AddFunctionActivity.class);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public void onBindFooterViewHolder(BaseViewHolder holder, int groupPosition) {
    }

    @Override
    public void onBindChildViewHolder(BaseViewHolder holder, int groupPosition, int childPosition) {
        FuncBtnTable entity = mGroups.get(groupPosition).getTeamList().get(childPosition);
        holder.getView(R.id.iv_func_pic).setBackgroundDrawable(mContext.getResources().getDrawable(entity.getPicId()));
        holder.setText(R.id.tv_func_name, entity.getName());
        if (isDelete) {
            //显示删除按钮
            holder.getView(R.id.iv_delete_btn).setVisibility(View.VISIBLE);
            Animation shake;
            if (childPosition % 2 == 1) {
                shake = AnimationUtils.loadAnimation(mContext, R.anim.anim_func);
            } else {
                shake = AnimationUtils.loadAnimation(mContext, R.anim.anim_func2);
            }
            shake.reset();
            shake.setFillAfter(true);
            holder.itemView.startAnimation(shake);
        } else {
            holder.getView(R.id.iv_delete_btn).setVisibility(View.GONE);
        }
        LinearLayout llFuncBtn = holder.getView(R.id.ll_func_btn);
        int deptType = SharedPreferUtil.getInstance().getIntValue(Common.DEPT_TYPE);
        if (deptType == 5 || deptType == 6) {
            if (entity.getNameCode().equals(Common.FUNC_QDGL) || entity.getNameCode().equals(Common.FUNC_LHSM)) {
                //分拨屏蔽签到管理、揽货扫描
                llFuncBtn.setEnabled(false);
            } else {
                llFuncBtn.setEnabled(true);
            }
        } else {
            if (entity.getNameCode().equals(Common.FUNC_FBZT)) {
                //网点屏蔽分拨自提
                llFuncBtn.setEnabled(false);
            } else {
                llFuncBtn.setEnabled(true);
            }
        }
    }

    public boolean isDelete() {
        return isDelete;
    }

    public void setDelete(boolean delete) {
        isDelete = delete;
    }
}
